package com.wind13;

import java.util.ArrayList;
import java.util.List;

public class Game {

    private final List<Rule> rules;
    private final Related related;

    /**
     * Numbers:
     * [ ] 3 Call:  __Love____
     * [ ] 5 Call:  __Hate____
     * [x] 7 Call:  __Good____
     *
     * Related:
     *
     * [x] Divided
     * [ ] Contains
     *
     *
     * @param rules The rules to create the game.
     * @param related The related setting to create the game.
     */
    public Game(List<Rule> rules, Related related){
        this.rules = rules;
        this.related = related;
    }

    public String fizzbuzz(int i) {
        final StringBuffer result = new StringBuffer();
        rules.forEach(rule -> {
            if (isRelated(i, rule.getNumber())) {
                result.append(rule.getResult());
            }
        });
        return result.length() == 0 ? intToString(i) : result.toString();
    }

    public boolean isRelated(int i, int related) {
        boolean divided = this.related.isDivided() && isDivided(i, related);
        boolean contains =this.related.isContains() && isContains(i, related);
        return divided || contains;
    }

    private boolean isContains(int i, int related) {
        return intToString(i).contains(intToString(related));
    }

    private boolean isDivided(int i, int related) {
        return i % related == 0;
    }

    private String intToString(int related) {
        return "" + related;
    }

    public void run(int max) {
        for (int i=0; i<max; i++) {
            System.out.printf("%d, %s\n", i, this.fizzbuzz(i));
        }
    }

    public static void main(String[] args) {
        final List<Rule> rules = new ArrayList<>();
        rules.add(new Rule(3, "fizz"));
        rules.add(new Rule(5, "buzz"));
        rules.add(new Rule(7, "mess"));
        Related related = new Related(true, true);
        Game game = new Game(rules, related);
        game.run(100);
    }
}
